package org.dreamwork.seagragon;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.OutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-4-24
 * Time: 22:24:37
 */
public class WebPageWorker extends Thread {
    private String url;
    private OutputStream out;
    private ServletContext context;

    public WebPageWorker (ServletContext context, OutputStream out, String url) {
        this.url = url;
        this.out = out;
        this.context = context;
    }

    @Override
    public void run () {
        RequestDispatcher dispatcher = context.getRequestDispatcher (url);
        if (dispatcher == null) {
            context.log ("Serivce not avialbe: " + url);
            return;
        }

        try {
            ImitationServletOutputStream os = new ImitationServletOutputStream (out);
            ImitationHttpRequest    request = new ImitationHttpRequest (context, url);
            ImitationHttpResponse  response = new ImitationHttpResponse (os);
            dispatcher.include (request, response);
        } catch (Exception ex) {
            context.log (ex.getMessage (), ex);
        }
    }
}